clear all
*cap log close
set more off


// build experience measure ------------
use "${data}/out/4-main", clear
keep officerid 
duplicates drop 
tempfile list 
save    `list'

use "${data}/out/3-officer", clear
merge 1:1 officerid using `list', keep(3) nogen
gen start_date = firstfhpft 
replace start_date = firstfhp if mi(start_date)
keep officerid start_date 
tempfile exper 
save    `exper'

use "${data}/out/4-main", clear
merge m:1 officerid using `exper', keep(3) nogen 
gen exper = (offensedate-start_date)/(365.25)
// --------------------------------------


// denote high vs. low experience ---------
summ exper, d
gen hiexp = (exper >= `r(p50)')


// trim dataset for analysis in R --------
gen D = harsh
gen W = covbin1 
gen jid = officerid 
keep  citationid cite_ny1 contest D Z W totfe jid hiexp lenient* 
order citationid cite_ny1 contest D Z W totfe jid hiexp lenient* 


// for experience heterogeneity: ------------------------
// require fe to have overlap ----------------
bysort totfe: egen count_hiexp = sum(hiexp==1)
bysort totfe: egen count_loexp = sum(hiexp==0)
keep if count_hiexp > 0 & count_loexp > 0
*drop count_* 

// weights for fe distribution ---------------
gen frac_hiexp = (count_hiexp)/(count_hiexp+count_loexp)
gen frac_loexp = (count_loexp)/(count_hiexp+count_loexp)
gen weight = frac_loexp/frac_hiexp

* require sufficient overlap
qui summ weight, d 
keep if weight > `r(p1)' & weight < `r(p99)'

gen rwt = . 
replace rwt = frac_loexp/frac_hiexp if hiexp==1 
replace rwt = 1 if hiexp==0

drop weight count_* frac_*
// ---------------------------------------------


// store dataset ---------------------------
save "${temp}/data_exper", replace 



